package com.ss.android.common.util;

import java.util.HashMap;

public class StringUtils {
	public static String clean(String str){
		int i=str.indexOf('"');
		int j=str.lastIndexOf('"');
		if((i>=0)&&(j>i)){
			str=str.substring(i+1,j).trim();
		}
		return str;
	}
	
	public static String getShortDateTime(String str){
		int i=1+str.indexOf('-');
		int j=str.lastIndexOf(':');
		if((i>0)&&(j>0)){
			str=str.substring(i,j);
		}
		return str;
	}
	
	private static boolean isChinese(char c){
		Character.UnicodeBlock unicodeBlock=Character.UnicodeBlock.of(c);
		return (unicodeBlock==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)||
				(unicodeBlock==Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS)||
				(unicodeBlock==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A)||
				(unicodeBlock==Character.UnicodeBlock.GENERAL_PUNCTUATION)||
				(unicodeBlock==Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION)||
				(unicodeBlock==Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS);
	}
	
	public static boolean isEmpty(String str){
		return (str==null)||(str.trim().length()==0);
	}
	
	public static HashMap<String,String> parseResponse(String str){
		int i=str.indexOf('{');
		int j=str.lastIndexOf('}');
		String[] arrStr=str.substring(i+1,j).split(",");
		for(int k=0;i<arrStr.length;k++){
			arrStr[k]=arrStr[k].trim();
		}
		HashMap<String,String> map=new HashMap<String,String>();
		for(int m=0;m<arrStr.length;m++){
			String[] subArr=arrStr[m].split(":",2);
			map.put(clean(subArr[0]), clean(subArr[1]));
		}
		return map;
	}
	
	public static String removeBlank(String str){
		if(isEmpty(str)){
			return "";
		}
		StringBuffer buffer=new StringBuffer();
		for(int i=0;i<str.length();i++){
			if((str.charAt(i)>='0')&&(str.charAt(i)<='9')){
				buffer.append(str.charAt(i));
			}
		}
		return buffer.toString();
	}
	
	public static String toDBC(String str){
		char[] arrChar=str.toCharArray();
		int i=0;
		while(i<arrChar.length){
			if(isChinese(arrChar[i])){
				if(arrChar[i]!='　'){
					if((arrChar[i]>65280)&&(arrChar[i]<65375)){
						arrChar[i]=((char)(arrChar[i]-65248));
					}
				}else{
					arrChar[i]=' ';
				}
			}
			i++;
		}
		return new String(arrChar);
	}
	
	public static String toSBC(String str){
		char[] arrChar=str.toCharArray();
		int i=0;
		while(i<arrChar.length){
			if(arrChar[i]==' '){
				arrChar[i]='　';
				break;
			}
			if((arrChar[i]<' ')&&(arrChar[i]>' ')){
				arrChar[i]=((char)(65248+arrChar[i]));
			}
			i++;
		}
		return new String(arrChar);
	}
}
